package com.ipt.frm.pw;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ipt.frm.bean.Alumno;
import com.ipt.frm.factoria.DAOFactory;
import com.ipt.frm.interfaces.InterfaceAlumnoDAO;

/**
 * Servlet implementation class servletRanking
 */
public class servletRanking extends HttpServlet {

	private static InterfaceAlumnoDAO aDao;

	private static ArrayList<Alumno> alumnos = new ArrayList<Alumno>();
	private static DAOFactory fMySQL;
	private static final long serialVersionUID = -5186174395080599897L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public servletRanking() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			fMySQL = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
			aDao = fMySQL.getInterfaceAlumnoDAO();
			alumnos = aDao.getAlumnosActivos("alumno");
			request.setAttribute("puntuaciones", alumnos);

			RequestDispatcher dispatcher = this.getServletContext()
					.getRequestDispatcher("/ranking.jsp");
			request.setAttribute("title", "Ranking de alumnos del dado");
			dispatcher.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
